package edu.cmu.mism.dgjava.views;

import java.io.IOException;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.IProgressService;

import edu.cmu.mism.dgjava.algorithm.impl.TreeNode;
import edu.cmu.mism.dgjava.data.ui.CommonParametersTypeBook;
import edu.cmu.mism.dgjava.data.ui.composites.AbstractDataComposite;
import edu.cmu.mism.dgjava.data.ui.tree.TreeDialog;
import edu.cmu.mism.dgjava.jobs.OptionCalculationJob;
import edu.cmu.mism.dgjava.jobs.OptionCalculationJobManager;
import edu.cmu.mism.dgjava.jobs.OptionCalculationJobRule;
import edu.cmu.mism.dgjava.utils.SWTUtils;

/**
 * Underlying Data View under Equity_FX_Index_Futures_Options perspective
 * 
 * @author Christian
 * 
 */
public class UnderlyingDataView extends ViewPart {
	public static final String ID = "DGJava.view.underlyingData"; //$NON-NLS-1$

	private CommonParametersTypeBook _underlyingTypeBook;
	private CommonParametersTypeBook _optionTypeBook;
	private Button _calBtn;
	private Button _showTreeBtn;
	private TreeNode[][] _tree;

	private ModifyListener _validator = new ModifyListener() {

		@Override
		public void modifyText(ModifyEvent e) {
			validate();
		}
	};

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	@Override
	public void createPartControl(Composite parent) {
		Properties utDataCompProperties = new Properties();
		Properties otDataCompProperties = new Properties();

		try {
			utDataCompProperties.load(AbstractDataComposite.class
					.getResourceAsStream("optionData.properties"));
			otDataCompProperties.load(AbstractDataComposite.class
					.getResourceAsStream("optionType.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		SashForm sashContainer = new SashForm(parent, SWT.VERTICAL);

		Composite uContainer = SWTUtils.createComposite(sashContainer, 1, -1,
				1, GridData.FILL_HORIZONTAL);

		uContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		// underlying type combo book page composite
		_underlyingTypeBook = new CommonParametersTypeBook(uContainer,
				Messages.UnderlyingDataView_label_underlyingType,
				utDataCompProperties);

		Composite oContainer = SWTUtils.createComposite(sashContainer, 1, -1,
				1, GridData.FILL_HORIZONTAL);

		oContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		// option type combo book page composite
		_optionTypeBook = new CommonParametersTypeBook(oContainer,
				"Option Data:", otDataCompProperties);

		// set sash weigh for display
		sashContainer.setWeights(new int[] { 43, 60 });

		// Calculate Button
		createCalculateButton(oContainer);

	}

	/**
	 * @param lContainer
	 */
	private void createCalculateButton(Composite container) {
		Composite btnContainer = SWTUtils.createComposite(container, 2, -1, -1);
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.CENTER;
		btnContainer.setLayoutData(gd);

		this._calBtn = SWTUtils.createPushButton(btnContainer,
				Messages.UnderlyingDataView_button_calculate);
		this._calBtn.addSelectionListener(new SelectionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
			 * .swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				final Object underlyingModel = _underlyingTypeBook
						.getDataObject();
				final Object pricingModel = _optionTypeBook.getDataObject();

				IProgressService service = PlatformUI.getWorkbench()
						.getProgressService();
				// instantiate a option calculation job to process the
				// calculation

				OptionCalculationJob j = OptionCalculationJobManager
						.getInstance().newOptionCalculationJob(underlyingModel,
								pricingModel);
				service.showInDialog(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell(), j);

				j.setRule(new OptionCalculationJobRule());
				j.schedule();

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});

		GridData btnGd = new GridData(SWT.FILL, SWT.NONE, false, false);
		btnGd.horizontalSpan = 1;
		btnGd.horizontalAlignment = GridData.CENTER;
		this._calBtn.setLayoutData(btnGd);

		this._showTreeBtn = SWTUtils
				.createPushButton(btnContainer, "Show Tree");
		this._showTreeBtn.addSelectionListener(new SelectionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
			 * .swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (_tree != null) {
					TreeDialog dlg = new TreeDialog(_tree);
					dlg.open();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});
		enableShowTreeButton(null);

		btnGd = new GridData(SWT.FILL, SWT.NONE, false, false);
		btnGd.horizontalSpan = 1;
		btnGd.horizontalAlignment = GridData.CENTER;
		this._showTreeBtn.setLayoutData(btnGd);
	}

	public void enableShowTreeButton(TreeNode[][] tree) {
		this._tree = tree;
		this._showTreeBtn.setEnabled(this._tree != null);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		// viewer.getControl().setFocus();
	}

	/**
	 * Validate User's Input
	 */
	public void validate() {
		// TODO
	}
}